﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using System.Data;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS_Global;
using vsm.CustomControl;

public partial class IFRS_WIN31_GL_LN_2E : PageBase
{
    #region "---------- Property -----------"

    public string Srcipt_SetValueGLAccount_GL = "";
    public string Alert_TextHeader = "";

    #endregion

    #region "---------- Events -----------"

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Gen_Srcipt_SetValueGLAccount_GL();
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            BindControlData();
            SetErrorMessage();
            BindData();

            if (Request.QueryString["GroupID"] != null)
            {
                hidGLGroup_ID.Value = Request.QueryString["GroupID"].ToString();
                txtGLGroup.Text = Request.QueryString["GroupName"].ToString();
            }
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        if (!CheckDuplicateGL(form_control))
            SaveData(false);
    }

    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN31_1S.aspx");
    }

    #endregion

    #region "---------- Methods -----------"

    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsResource.Edit + Resources.ifrsMenu.WIN04;
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqGLAccount.SetRequiredField(lblGLAccount, txtGLAccount, btnSAVE);
    }

    private void BindControlData()
    {
        this.SetTextboxAutoComplete(form_control);
    }

    private void BindData()
    {
        if (Request.QueryString["id"] == null)
            return;

        int ID = Convert.ToInt32(Request.QueryString["id"].ToString());
        List<GLAccountPosting> glAccountPosting = new List<GLAccountPosting>();
        DataTable dt = new SPR_GLACCOUNTPOSTING().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, glAccountPosting);
            txtGLAccount.Text = glAccountPosting[0].GLAccount_CODE;
            txtGL_ADJ_INT_INC_PL.Text = glAccountPosting[0].GL_ADJ_INT_INC_PL_CODE;
            txtGL_ADJ_FV_BS.Text = glAccountPosting[0].GL_ADJ_FV_BS_CODE;
            txtGL_ADJ_INT_INC_ELCLS_PL.Text = glAccountPosting[0].GL_ADJ_INT_INC_ELCLS_PL_CODE;
            txtGL_ADJ_CST_BS.Text = glAccountPosting[0].GL_ADJ_CST_BS_CODE;
            txtGL_REV_CST_PL.Text = glAccountPosting[0].GL_REV_CST_PL_CODE;
            txtGL_ADJ_FEE_BS.Text = glAccountPosting[0].GL_ADJ_FEE_BS_CODE;
            txtGL_REV_FEE_PL.Text = glAccountPosting[0].GL_REV_FEE_PL_CODE;
            txtGL_SUBSEQ_ADJ_CST_BS.Text = glAccountPosting[0].GL_SUBSEQ_ADJ_CST_BS_CODE;
            txtGL_SUBSEQ_ADJ_FEE_BS.Text = glAccountPosting[0].GL_SUBSEQ_ADJ_FEE_BS_CODE;
            txtGL_IMP_LOSS_PL.Text = glAccountPosting[0].GL_IMP_LOSS_PL_CODE;
            txtGL_ALLW_BAD_DEBT_PL.Text = glAccountPosting[0].GL_ALLW_BAD_DEBT_PL_CODE;
            txtGL_WOFF_ADJ_FV_BS.Text = glAccountPosting[0].GL_WOFF_ADJ_FV_BS_CODE;
            txtGL_WOFF_ADJ_FEE_BS.Text = glAccountPosting[0].GL_WOFF_ADJ_FEE_BS_CODE;
            txtGL_WOFF_ADJ_CST_BS.Text = glAccountPosting[0].GL_WOFF_ADJ_CST_BS_CODE;
            txtGL_DAY1_FV_LOSS_PL.Text = glAccountPosting[0].GL_DAY1_FV_LOSS_PL_CODE;
            txtGL_DAY1_ADJ_FV_BS.Text = glAccountPosting[0].GL_DAY1_ADJ_FV_BS_CODE;
            txtGL_WOFF_INT_DISC_LN.Text = glAccountPosting[0].GL_WOFF_INT_DISC_LN_CODE;
            txtGL_DAY1_DEF_EXP_STAFF_BS.Text = glAccountPosting[0].GL_DAY1_DEF_EXP_STAFF_BS_CODE;
            txtGL_AMC_EMP_CST_PL.Text = glAccountPosting[0].GL_AMC_EMP_CST_PL_CODE;
            txtGL_AMC_DEF_EXP_STAFF_BS.Text = glAccountPosting[0].GL_AMC_DEF_EXP_STAFF_BS_CODE;
            txtGL_SUBSIDY_INT_REC_BS.Text = glAccountPosting[0].GL_SUBSIDY_INT_REC_BS_CODE;
            txtGL_SUBSIDY_INT_INC_PL.Text = glAccountPosting[0].GL_SUBSIDY_INT_INC_PL_CODE;


            txtGLGroup.Text = glAccountPosting[0].GLGroup_NAME;
            txtGLAccount_NAME.Text = glAccountPosting[0].GLAccount_NAME;
            txtGL_ADJ_INT_INC_PL_NAME.Text = glAccountPosting[0].GL_ADJ_INT_INC_PL_NAME;
            txtGL_ADJ_FV_BS_NAME.Text = glAccountPosting[0].GL_ADJ_FV_BS_NAME;
            txtGL_ADJ_INT_INC_ELCLS_PL_NAME.Text = glAccountPosting[0].GL_ADJ_INT_INC_ELCLS_PL_NAME;
            txtGL_ADJ_CST_BS_NAME.Text = glAccountPosting[0].GL_ADJ_CST_BS_NAME;
            txtGL_REV_CST_PL_NAME.Text = glAccountPosting[0].GL_REV_CST_PL_NAME;
            txtGL_ADJ_FEE_BS_NAME.Text = glAccountPosting[0].GL_ADJ_FEE_BS_NAME;
            txtGL_REV_FEE_PL_NAME.Text = glAccountPosting[0].GL_REV_FEE_PL_NAME;
            txtGL_SUBSEQ_ADJ_CST_BS_NAME.Text = glAccountPosting[0].GL_SUBSEQ_ADJ_CST_BS_NAME;
            txtGL_SUBSEQ_ADJ_FEE_BS_NAME.Text = glAccountPosting[0].GL_SUBSEQ_ADJ_FEE_BS_NAME;
            txtGL_IMP_LOSS_PL_NAME.Text = glAccountPosting[0].GL_IMP_LOSS_PL_NAME;
            txtGL_ALLW_BAD_DEBT_PL_NAME.Text = glAccountPosting[0].GL_ALLW_BAD_DEBT_PL_NAME;
            txtGL_WOFF_ADJ_FV_BS_NAME.Text = glAccountPosting[0].GL_WOFF_ADJ_FV_BS_NAME;
            txtGL_WOFF_ADJ_FEE_BS_NAME.Text = glAccountPosting[0].GL_WOFF_ADJ_FEE_BS_NAME;
            txtGL_WOFF_ADJ_CST_BS_NAME.Text = glAccountPosting[0].GL_WOFF_ADJ_CST_BS_NAME;
            txtGL_DAY1_FV_LOSS_PL_NAME.Text = glAccountPosting[0].GL_DAY1_FV_LOSS_PL_NAME;
            txtGL_DAY1_ADJ_FV_BS_NAME.Text = glAccountPosting[0].GL_DAY1_ADJ_FV_BS_NAME;
            txtGL_WOFF_INT_DISC_LN_NAME.Text = glAccountPosting[0].GL_WOFF_INT_DISC_LN_NAME;
            txtGL_DAY1_DEF_EXP_STAFF_BS_NAME.Text = glAccountPosting[0].GL_DAY1_DEF_EXP_STAFF_BS_NAME;
            txtGL_AMC_EMP_CST_PL_NAME.Text = glAccountPosting[0].GL_AMC_EMP_CST_PL_NAME;
            txtGL_AMC_DEF_EXP_STAFF_BS_NAME.Text = glAccountPosting[0].GL_AMC_DEF_EXP_STAFF_BS_NAME;
            txtGL_SUBSIDY_INT_REC_BS_NAME.Text = glAccountPosting[0].GL_SUBSIDY_INT_REC_BS_NAME;
            txtGL_SUBSIDY_INT_INC_PL_NAME.Text = glAccountPosting[0].GL_SUBSIDY_INT_INC_PL_NAME;

            hidGLAccountPostingID.Value = glAccountPosting[0].GLAccountPostingID.IntToString();
            hidGLGroup_ID.Value = glAccountPosting[0].GLGroupID.IntToString();
            hidGLAccount.Value = glAccountPosting[0].GLAccountID.IntToString();
            hidGL_ADJ_INT_INC_PL.Value = glAccountPosting[0].GL_ADJ_INT_INC_PL_ID.IntToString();
            hidGL_ADJ_FV_BS.Value = glAccountPosting[0].GL_ADJ_FV_BS_ID.IntToString();
            hidGL_ADJ_INT_INC_ELCLS_PL.Value = glAccountPosting[0].GL_ADJ_INT_INC_ELCLS_PL_ID.IntToString();
            hidGL_ADJ_CST_BS.Value = glAccountPosting[0].GL_ADJ_CST_BS_ID.IntToString();
            hidGL_REV_CST_PL.Value = glAccountPosting[0].GL_REV_CST_PL_ID.IntToString();
            hidGL_ADJ_FEE_BS.Value = glAccountPosting[0].GL_ADJ_FEE_BS_ID.IntToString();
            hidGL_REV_FEE_PL.Value = glAccountPosting[0].GL_REV_FEE_PL_ID.IntToString();
            hidGL_SUBSEQ_ADJ_CST_BS.Value = glAccountPosting[0].GL_SUBSEQ_ADJ_CST_BS_ID.IntToString();
            hidGL_SUBSEQ_ADJ_FEE_BS.Value = glAccountPosting[0].GL_SUBSEQ_ADJ_FEE_BS_ID.IntToString();
            hidGL_IMP_LOSS_PL.Value = glAccountPosting[0].GL_IMP_LOSS_PL_ID.IntToString();
            hidGL_ALLW_BAD_DEBT_PL.Value = glAccountPosting[0].GL_ALLW_BAD_DEBT_PL_ID.IntToString();
            hidGL_WOFF_ADJ_FV_BS.Value = glAccountPosting[0].GL_WOFF_ADJ_FV_BS_ID.IntToString();
            hidGL_WOFF_ADJ_FEE_BS.Value = glAccountPosting[0].GL_WOFF_ADJ_FEE_BS_ID.IntToString();
            hidGL_WOFF_ADJ_CST_BS.Value = glAccountPosting[0].GL_WOFF_ADJ_CST_BS_ID.IntToString();
            hidGL_DAY1_FV_LOSS_PL.Value = glAccountPosting[0].GL_DAY1_FV_LOSS_PL_ID.IntToString();
            hidGL_DAY1_ADJ_FV_BS.Value = glAccountPosting[0].GL_DAY1_ADJ_FV_BS_ID.IntToString();
            hidGL_WOFF_INT_DISC_LN.Value = glAccountPosting[0].GL_WOFF_INT_DISC_LN_ID.IntToString();
            hidGL_DAY1_DEF_EXP_STAFF_BS.Value = glAccountPosting[0].GL_DAY1_DEF_EXP_STAFF_BS_ID.IntToString();
            hidGL_AMC_EMP_CST_PL.Value = glAccountPosting[0].GL_AMC_EMP_CST_PL_ID.IntToString();
            hidGL_AMC_DEF_EXP_STAFF_BS.Value = glAccountPosting[0].GL_AMC_DEF_EXP_STAFF_BS_ID.IntToString();
            hidGL_SUBSIDY_INT_REC_BS.Value = glAccountPosting[0].GL_SUBSIDY_INT_REC_BS_ID.IntToString();
            hidGL_SUBSIDY_INT_INC_PL.Value = glAccountPosting[0].GL_SUBSIDY_INT_INC_PL_ID.IntToString();
        }
    }

    private void SaveData(bool SaveNew)
    {
        try
        {
            GLAccountPosting vGLAccountPosting = new GLAccountPosting();
            vGLAccountPosting.GLAccountPostingID = hidGLAccountPostingID.Value.AsInt32();
            vGLAccountPosting.GLGroupID = hidGLGroup_ID.Value.AsIntIsnull();
            vGLAccountPosting.GLAccountID = hidGLAccount.Value.AsIntIsnull();
            vGLAccountPosting.GL_ADJ_INT_INC_PL_ID = hidGL_ADJ_INT_INC_PL.Value.AsIntIsnull();
            vGLAccountPosting.GL_ADJ_FV_BS_ID = hidGL_ADJ_FV_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_ADJ_INT_INC_ELCLS_PL_ID = hidGL_ADJ_INT_INC_ELCLS_PL.Value.AsIntIsnull();
            vGLAccountPosting.GL_ADJ_CST_BS_ID = hidGL_ADJ_CST_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_REV_CST_PL_ID = hidGL_REV_CST_PL.Value.AsIntIsnull();
            vGLAccountPosting.GL_ADJ_FEE_BS_ID = hidGL_ADJ_FEE_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_REV_FEE_PL_ID = hidGL_REV_FEE_PL.Value.AsIntIsnull();
            vGLAccountPosting.GL_SUBSEQ_ADJ_CST_BS_ID = hidGL_SUBSEQ_ADJ_CST_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_SUBSEQ_ADJ_FEE_BS_ID = hidGL_SUBSEQ_ADJ_FEE_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_IMP_LOSS_PL_ID = hidGL_IMP_LOSS_PL.Value.AsIntIsnull();
            vGLAccountPosting.GL_ALLW_BAD_DEBT_PL_ID = hidGL_ALLW_BAD_DEBT_PL.Value.AsIntIsnull();
            vGLAccountPosting.GL_WOFF_ADJ_FV_BS_ID = hidGL_WOFF_ADJ_FV_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_WOFF_ADJ_FEE_BS_ID = hidGL_WOFF_ADJ_FEE_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_WOFF_ADJ_CST_BS_ID = hidGL_WOFF_ADJ_CST_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_DAY1_FV_LOSS_PL_ID = hidGL_DAY1_FV_LOSS_PL.Value.AsIntIsnull();
            vGLAccountPosting.GL_DAY1_ADJ_FV_BS_ID = hidGL_DAY1_ADJ_FV_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_WOFF_INT_DISC_LN_ID = hidGL_WOFF_INT_DISC_LN.Value.AsIntIsnull();
            vGLAccountPosting.GL_DAY1_DEF_EXP_STAFF_BS_ID = hidGL_DAY1_DEF_EXP_STAFF_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_AMC_EMP_CST_PL_ID = hidGL_AMC_EMP_CST_PL.Value.AsIntIsnull();
            vGLAccountPosting.GL_AMC_DEF_EXP_STAFF_BS_ID = hidGL_AMC_DEF_EXP_STAFF_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_SUBSIDY_INT_REC_BS_ID = hidGL_SUBSIDY_INT_REC_BS.Value.AsIntIsnull();
            vGLAccountPosting.GL_SUBSIDY_INT_INC_PL_ID = hidGL_SUBSIDY_INT_INC_PL.Value.AsIntIsnull();
            vGLAccountPosting.SetStandardField();

            int iID = 0;
            var SPR_GLACCOUNTPOSTING = new SPR_GLACCOUNTPOSTING();
            iID = SPR_GLACCOUNTPOSTING.UPDATE(vGLAccountPosting);
            if (iID > 0)
            {
                if (SaveNew)
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsoluteUri));
                else
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN31_GL_LN_2E.aspx?&id={0}", hidGLAccountPostingID.Value)));
            }
            else
            {
                if (iID == -1)
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                else
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void Gen_Srcipt_SetValueGLAccount_GL()
    {
        // Head
        Srcipt_SetValueGLAccount_GL += "<script type='text/javascript'>window.SetValueGLAccount_GL = function (Value1, Value2, Value3, Value4) {  ";

        foreach (Control ctrl in form_control.Controls)
        {
            if (ctrl is TextboxAutoComplete)
            {
                var txtCode = ((TextboxAutoComplete)ctrl).ID;
                var hidName = "hid" + ((TextboxAutoComplete)ctrl).ID.Substring(3);
                var txtName = ((TextboxAutoComplete)ctrl).ID + "_NAME";
                TextBox Control_txtCode = (TextBox)form_control.FindControl(txtCode);
                TextBox Control_txtName = (TextBox)form_control.FindControl(txtName);
                HiddenField Control_hidName = (HiddenField)form_control.FindControl(hidName);

                Srcipt_SetValueGLAccount_GL += String.Format("if('{0}' == Value4)", txtCode.Substring(3));
                Srcipt_SetValueGLAccount_GL += " {";
                Srcipt_SetValueGLAccount_GL += String.Format("document.getElementById('{0}').value = Value1;", Control_hidName.ClientID);
                Srcipt_SetValueGLAccount_GL += String.Format("document.getElementById('{0}').value = Value2;", Control_txtCode.ClientID);
                Srcipt_SetValueGLAccount_GL += String.Format("document.getElementById('{0}').value = Value3;", Control_txtName.ClientID);
                Srcipt_SetValueGLAccount_GL += " }";
            }
        }

        // Last
        Srcipt_SetValueGLAccount_GL += " }</script>";
    }

    // Loop SetControl TextboxAutoComplete and Button
    private void SetTextboxAutoComplete(PlaceHolder plcReadOnly)
    {
        foreach (Control ctrl in plcReadOnly.Controls)
        {
            if (ctrl is TextboxAutoComplete)
            {
                var txtCode = ((TextboxAutoComplete)ctrl).ID;
                var hidName = "hid" + ((TextboxAutoComplete)ctrl).ID.Substring(3);
                var txtName = ((TextboxAutoComplete)ctrl).ID + "_NAME";
                HiddenField hidControl = (HiddenField)plcReadOnly.FindControl(hidName);
                TextBox txtControl = (TextBox)plcReadOnly.FindControl(txtName);

                ((TextboxAutoComplete)ctrl).AddControl(hidControl.ClientID);
                ((TextboxAutoComplete)ctrl).AddControl(ctrl.ClientID);
                ((TextboxAutoComplete)ctrl).AddControl(txtControl.ClientID);
                ((TextboxAutoComplete)ctrl).ControlSearch = ctrl.ClientID;
                ((TextboxAutoComplete)ctrl).AddQueryString("bIsActive", "1");
                ((TextboxAutoComplete)ctrl).SearchKey = "GLAccount_oGLAccountCode_oGLAccountName_GL";
            }
            if (ctrl is Button)
            {
                var txtCode = ((Button)ctrl).ID.Substring(3);
                ((Button)ctrl).Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount_GL.aspx?PopUp=" + txtCode) + "','700','550');");
            }
        }
    }

    private bool CheckDuplicateGL(PlaceHolder plc)
    {
        string Alert_TextDetail = "";
        List<HiddenGL> lst_hidden = new List<HiddenGL>();
        foreach (Control ctrl in plc.Controls)
        {
            if (ctrl is HiddenField)
            {
                if (((HiddenField)ctrl).Value != "")
                    lst_hidden.Add(new HiddenGL { HiddenID = ((HiddenField)ctrl).ID, Value = ((HiddenField)ctrl).Value });
            }
        }

        var tmp = "";
        for (int i = 0; i < lst_hidden.Count; i++)
        {
            tmp = "";
            var dup_value = lst_hidden[i].Value; // ค่า Value ที่เช็คซ้ำ
            var lst = lst_hidden.Where(x => x.Value == dup_value && x.Value != "" && x.HiddenID != lst_hidden[i].HiddenID).ToList();
            foreach (var row in lst)
            {
                Label lbl_control = (Label)plc.FindControl("lbl" + row.HiddenID.Substring(3));
                tmp += lbl_control.Text + ", ";
            }
            if (lst.Count != 0 && i < lst_hidden.Count - 1)
            {
                Alert_TextDetail += "- " + ((Label)plc.FindControl("lbl" + lst_hidden[i].HiddenID.Substring(3))).Text + ", ";
                Alert_TextDetail += tmp.Substring(0, tmp.Length - 2) + "\\n";
            }

            for (int j = 0; j < lst_hidden.Count; j++)
            {
                // Clear ค่า Value ใน List<HiddenGL> ที่ซ้ำ
                if (lst_hidden[j].Value == dup_value)
                    lst_hidden[j].Value = "";
            }
        }

        if (Alert_TextDetail == "")
        {
            return false;
        }
        else
        {
            var script_alert = Resources.ifrsResource.Alert_TextHeaderGL + Alert_TextDetail;
            Utility.AlertMessage(Page, script_alert);
            return true;
        }
    }

    public class HiddenGL
    {
        public string HiddenID { get; set; }
        public string Value { get; set; }
    }

    #endregion
}